package com.example.renyang.happysell.Tools;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.renyang.happysell.MyUntil.Car;
import com.example.renyang.happysell.MyUntil.food_detail;
import com.example.renyang.happysell.R;
import com.example.renyang.happysell.net.HttpUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import infClass.Food;

public class msell_food extends AppCompatActivity {
    private Handler hander;
   static String uname=null;
    static String storename=null;
    private ListView lv;
    final JSONArray[] obj = {new JSONArray()};
    List<Food> foods=new ArrayList<Food>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msell_food);
        lv=(ListView)findViewById(R.id.search_listview_text);
        Intent intent=getIntent();
        uname=intent.getStringExtra("sname");
        storename=intent.getStringExtra("storename");

        new Thread(){
            public void run(){
                try {

                    init(uname,storename);
                    Log.i("zx",uname);
                }catch (Exception e){
                    e.printStackTrace();

                }
            }
        }.start();
        hander=new android.os.Handler(){
            @Override
            public void handleMessage(Message msg) {

                switch (msg.what){
                    case 1:
                        add();
                        break;
                    default:break;
                }
            }
        };

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Food food = foods.get(position);

                Log.i("zx", "*********************1" + food.getFname());
                Intent intent1 = new Intent(msell_food.this, food_detail.class);
                Bundle bundle = new Bundle();
                bundle.putString("name", food.getFname());
                bundle.putString("price", food.getPrice());
                bundle.putString("storename", food.getStorename());
                bundle.putString("info", food.getIntro());
                intent1.putExtras(bundle);

                startActivity(intent1);
            }
        });

        Log.i("zx","+++++++++++++++++++++++++++++++++++++++++++++--------------------------"+Car.getCarlist().size());
    }

    public void init(String name,String storename) throws  Exception{
        try {
            obj[0] =queryini(name,storename);
            JSONArray array = obj[0];
            for(int i=0;i<array.length();i++){
               Food food=new Food();
                JSONObject jsonObject=new JSONObject(array.get(i).toString());
                food.setFname(jsonObject.get("fname").toString());
                food.setPrice(jsonObject.get("price").toString());
   //             food.setIntro(jsonObject.get("intro").toString());

                food.setStorename(jsonObject.get("storename").toString());
               Car.addcarlist(food);
                Log.i("zx", "111111111111111111111111111" + food.getFname());
                foods.add(food);
      //    Log.i("zx","lllllllllllllllllllll2222222222222222222222222222"+"food"+Car.getCarlist().size());
             //   Car.addcarlist(food);
            }
            Message m=new Message();
            m.what=1;
            hander.sendMessage(m);
        }catch (Exception e){
            e.printStackTrace();
            Log.i("zx","访问出错");
        }
    }
    private JSONArray queryini(String name,String storename) throws  Exception{


        String url = HttpUtil.BASE_URL + "getFoods?storename="+storename;
        String s = HttpUtil.getRequest(url);
        Log.i("zx",url+"");
        return new JSONArray(s);
    }
    public void add(){

        ArrayList<HashMap<String,Object>> listItem=new ArrayList<HashMap<String, Object>>();
        for(int i=0;i<foods.size();i++){
            HashMap<String,Object> map=new HashMap<String, Object>();
            map.put("title", ":" + foods.get(i).getFname());
            map.put("title1","");
            map.put("title2",":"+foods.get(i).getPrice());
            listItem.add(map);
        };
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItem,R.layout.items,new String[]{"image","title","title1","title2"},
                new int[]{R.id.image,R.id.title,R.id.title1,R.id.title2});
        lv.setAdapter(simpleAdapter);
    }
}
